<?php

namespace backend\modules\member\controllers;

use Yii;
use common\models\base\SearchModel;
use backend\controllers\BaseController;
use common\models\forms\CreditsLogForm;
use common\models\member\MemberWithdraw;


class WithdrawController extends BaseController
{
    /**
     * @var \yii\db\MemberWithdraw
     */
    public $modelClass = MemberWithdraw::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['username'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);

        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }

    public function actionSuo()
    {  
      //锁定
        $id=Yii::$app->request->get('id');
        $status=Yii::$app->request->get('status');
        $model=MemberWithdraw::find()->where(['id'=>$id])->andWhere(['<=','status',1])->all();
        $connection = Yii::$app->db;
        $transaction = $connection->beginTransaction();
        try {
            foreach ($model as $key => $v) { 
                $v->status=$status;
                if (!$v->update()) {
                        throw new \Exception("审核失败:".$this->getError($v));
                }
            }
            $transaction->commit();/*提交事物*/
            return $this->message('成功', $this->redirect(Yii::$app->request->referrer), 'success');
        }
        catch (\Exception $e) {
            $transaction->rollBack();/*回滚*/
            return $this->message($e->getMessage(), $this->redirect(Yii::$app->request->referrer), 'error');
       }
    }

    public function actionView()
    {
        $id=Yii::$app->request->get('id');
        $model = $this->modelClass::findOne($id);
        return $this->render('view', [
            'model' => $model,
        ]);
    }

    public function actionCheckAccount(){
        if(Yii::$app->request->post()){

            $id=yii::$app->request->post('id');
            $model=MemberWithdraw::find()->where(['id'=>$id])->andWhere(['status'=>1])->one();
            $model->status=2;
            $model->confirm_at=time();
            if (!$model->update()) {
                return $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'success');
            }
            return $this->message('审核成功', $this->redirect(Yii::$app->request->referrer), 'success');
        }
        
    }
    public function actionRefuse(){
        $id=yii::$app->request->post('id');
        $cont=yii::$app->request->post('con');
        $connection = Yii::$app->db;
        $transaction = $connection->beginTransaction();
        $model = MemberWithdraw::find()->where(['id' => $id])->andWhere(['status' => 1])->one();

        try {
            $model->status=3;
            $model->confirm_at=time();
            $model->note=$cont;//打回原因
            if (!$model->update()) {
                throw new \Exception("打回失败");
            }
            Yii::$app->services->memberCreditsLog->incrMoney(new CreditsLogForm([
                'member' => $model->member,
                'num' => $model->amount,
                'credit_group' => 'withdraw',
                'remark' => 'withdraw return'
            ]));
            $transaction->commit();/*提交事物*/
            return $this->message('打回成功', $this->redirect(Yii::$app->request->referrer), 'success');
        }
        catch (\Exception $e) {
            $transaction->rollBack();/*回滚*/
            return $this->message($e->getMessage(), $this->redirect(Yii::$app->request->referrer), 'error');
        }
    }
}